var that,
  api = new(require('../../utils/api.js')),
  headimg = api.getimgurl(),
  staticimg = api.getstaticimgurl(),
  QQMapWX = require('../../utils/qqmap-wx-jssdk.js'),
qqmapsdk,
  util = new(require('../../utils/util.js')),
  tabbars=['ALL','NO_PAY','PAYSUCCESS','SHIPPED','RECEIVED'];
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pagesize:15,start:0,state:'ALL',
    tabindex:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    that = this;
    let userid = wx.getStorageSync('user').id,{tabindex}=that.data;
    that.setData({userid})
    that.getlist(tabindex)
    qqmapsdk = new QQMapWX({
      key: getApp().globalData.mapkey // 必填
    });
  },
  call(e){
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.phone,
    })
  },
  tonav(e){
    let plugin = requirePlugin('routePlan');
    let key = 'CB5BZ-6NP6W-KQGRJ-OENGJ-AD74O-4FFMC';  //使用在腾讯位置服务申请的key
    let referer = '回收小程序';   //调用插件的app的名称
    let endPoint = JSON.stringify({  //终点
      
      'name': e.currentTarget.dataset.address,
      'latitude': parseFloat(e.currentTarget.dataset.lat) ,
      'longitude': parseFloat(e.currentTarget.dataset.lng)
    });
    wx.navigateTo({
      url: 'plugin://routePlan/index?key=' + key + '&referer=' + referer + '&endPoint=' + endPoint+'&navigation=1'
    });
  },
  tabtap(e){
    if (this.data.tabindex != e.currentTarget.dataset.index)
    this.getlist(e.currentTarget.dataset.index)
  },
  getlist(tabindex){
    wx.showLoading({
      title: '加载中',
    })
    let{pagesize,start,state,userid} = this.data,data = {pagesize,start,state: tabbars[tabindex],userid};
    console.log(data,'data');
    
    api.getseoreorderslist(data,res=>{
      wx.hideLoading({
        complete: (res) => {},
      })
      if(res.data.issuccess==1){
        let{list} = res.data;
        list.forEach((x,i)=>{
          //NO_PAY 未支付 PAYSUCCESS 待发货 SHIPPED 已发货  RECEIVED 已收货  
          list[i]['apply']['statecontent'] = util.getgoodstype(x.apply.state)
          list[i]['apply']['date'] =util.formatTimenosecend(new Date(x.apply.updateDate)) 
          list[i]['apply']['addressinfo']=JSON.parse(util.decodeurl(x.apply.addressinfo)) 
          list[i]['topimg']=headimg+list[i]['scoregoods'].lbimg.split(",")[0]
        })
        that.setData({list,tabindex})
      }else{
        wx.showModal({
          title: '提示',
          showCancel:!1,
          content:res.data.msg
        })
      }
    })
  },
  tocheckcode(e){
    that.setData({selectitem:e.currentTarget.dataset.item},()=>{
      wx.navigateTo({
        url: '../checkcode/checkcode',
      })
    })
  },
  toassess(e){
      wx.navigateTo({
        url: '../assess/assess?num='+e.currentTarget.dataset.ordernum,
      })
  },
  toaddress(e){
    let{lat,lng}= e.currentTarget.dataset;
    wx.openLocation({
      latitude:Number(lat) ,
      longitude: Number(lng),
    })
  },
  copy(e){
    wx.setClipboardData({
      data: e.currentTarget.dataset.code,
    })
  },
  todetail(e){
    var { ordernum } = e.currentTarget.dataset;
  //  console.log(ordernumber,"ordernumber");
    wx.navigateTo({
      url: `../orderdetail/orderdetail?ordernum=${ordernum}`,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(that.data.needfresh){
      that.getlist()
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})